import time
import os
import re
from common.save_records import SaveFile, MySQL
from common.pre_config_brower import request_fun


def main(excel_file_name, mysql_instance, categoryInfo):
    print("Task:%s..is staring" % excel_file_name)
    create_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))

    log_dir = "D:\\pycode\\JT_spider\\%s" % excel_file_name
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    excel_file_full_path_name = os.path.join(log_dir, '%s_%s.xlsx' % (excel_file_name, create_time))
    produce_img_dir_path = "%s\\img" % log_dir

    save_files = SaveFile(excel_file_full_path_name, produce_img_dir_path)
    excel_file, excel_file_format, file_object = save_files.create_excel()
    if not os.path.exists(produce_img_dir_path):
        os.mkdir(produce_img_dir_path)

    __update_items_to_sql(mysql_instance(), categoryInfo)

    selected = "productPicUrl,productName,productPrice,productUrl"
    where_condition = "category2='%s'" % categoryInfo["category2"]
    product_list = mysql_instance().db_query(selected, where_str=where_condition)

    row_num = 1
    for info in product_list:
        img_path = save_files.download_img(info["productPicUrl"])
        excel_file.set_row(row_num, 250)
        excel_file.insert_image(row_num, 0, img_path)
        for index, name in enumerate(["productName", "productPrice", "productUrl"]):
            excel_file.write(row_num, index+1, info[name], excel_file_format)
        row_num += 1
    file_object.close()


def __download_img(savePath, url):
    pass


def __get_items_from_web(product_url):
    content = request_fun(product_url)
    items = re.findall(r'<a href="(.*?)"><img src="(.*?)" alt="(.*?)"/><p>.*?</p><p>(.*?)</p></a>', content.decode())
    print(len(items))
    return items


def __update_items_to_sql(sql_instance, categoryInfo):
    for info in __get_items_from_web(categoryInfo["url"]):
        items = {}
        items["productName"] = info[2],
        items["productUrl"] = info[0],
        items["productPrice"] = info[3],
        if info[1] != "null":
            items["productPicUrl"] = info[1],
        else:
            continue
        items["productPicPath"] = "None",
        items["productMore"] = "None"

        items.update(categoryInfo)
        sql_instance.db_insert(items)

    sql_instance.db_close()


def __mysql_instance():
    con_sql_info = {'host': '49.235.84.115',
                    'port': 6606,
                    'username': 'root',
                    'passwd': 'bxf198592',
                    'db_name': 'jetSpider'}
    return MySQL(**con_sql_info)


if __name__ == "__main__":
    companyName = "zara"
    category = "christams"
    subCat = {
        "decoration": "https://www.zarahome.com/gb/christmas/decoration-c1020189515.html",
        "TREE-ORNAMENTS": "https://www.zarahome.com/gb/christmas/tree-ornaments-c1020194534.html",
        "bed-room": "https://www.zarahome.com/gb/christmas/bedroom-c1020193054.html",
        "kitchen": "https://www.zarahome.com/gb/christmas/kitchen-c1020194541.html",
        "dining": "https://www.zarahome.com/gb/christmas/dining-c1524021.html",
        "gift-wrap": "https://www.zarahome.com/gb/christmas/gift-wrap-%26-cards-c1020313503.html",
        "fragrances": "https://www.zarahome.com/gb/christmas/fragrances-c1524020.html",
        "kids": "https://www.zarahome.com/gb/christmas/kids-c1524019.html"
    }

    categoryInfo = {
        "companyName": companyName,
        "category": category
    }

    for category2, url in subCat.items():
        start_time = time.time()
        print(url)

        categoryInfo["category2"] = category2
        categoryInfo["url"] = url

        __mysql_instance().db_insert(categoryInfo, info="product")
        main(category2, __mysql_instance, categoryInfo)

        print("Spend time: %s" % (time.time() - start_time))
